package com.example.act6.common.entities;

import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
@NoArgsConstructor
public class CommonResult<T> implements Serializable {  //泛型：如果装的userinfo 返回userinfo,装的order 返回order

    private Integer code;
    private String message;
    private T data;
    private String traceId;

    public CommonResult(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public CommonResult(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static <T> CommonResult<T> ok() {
        CommonResult<T> r = new CommonResult<>();
        r.setCode(0);
        r.setMessage("操作成功!");
        return r;
    }

    public static <T> CommonResult<T> ok(String message, T t) {
        CommonResult<T> r = new CommonResult<>();
        r.setCode(0);
        r.setMessage(message);
        r.setData(t);
        return r;
    }

    public static <T> CommonResult<T> ok(T t) {
        CommonResult<T> r = new CommonResult<>();
        r.setCode(0);
        r.setMessage("操作成功!");
        r.setData(t);
        return r;
    }

    public static <T> CommonResult<T> error() {
        CommonResult<T> r = new CommonResult<>();
        r.setCode(-1);
        r.setMessage("操作失败!");
        return r;
    }

    public static <T> CommonResult<T> error(String msg) {
        CommonResult<T> r = new CommonResult<>();
        r.setCode(-1);
        r.setMessage(msg);
        return r;
    }

    public static <T> CommonResult<T> error(T t) {
        CommonResult<T> r = new CommonResult<>();
        r.setCode(-1);
        r.setMessage("操作失败!");
        r.setData(t);
        return r;
    }

    public static <T> CommonResult<T> error(int code, String msg) {
        CommonResult<T> r = new CommonResult<>();
        r.setCode(code);
        r.setMessage(msg);
        return r;
    }

    public static <T> CommonResult<T> error(String message, T t) {
        CommonResult<T> r = new CommonResult<>();
        r.setCode(-1);
        r.setMessage(message);
        r.setData(t);
        return r;
    }

    public static <T> CommonResult<T> error(int code, String message, T t) {
        CommonResult<T> r = new CommonResult<>();
        r.setCode(code == 0 ? -1 : code);
        r.setMessage(message);
        r.setData(t);
        return r;
    }

    /**
     * 前端根据不同的code进行逻辑操作
     */
    public static <T> CommonResult<T> action(int code, String msg) {
        CommonResult<T> r = new CommonResult<>();
        r.setCode(code);
        r.setMessage(msg);
        return r;
    }

    /**
     * 前端根据不同的code进行逻辑操作
     */
    public static <T> CommonResult<T> action(int code, String message, T t) {
        CommonResult<T> r = new CommonResult<>();
        r.setCode(code);
        r.setMessage(message);
        r.setData(t);
        return r;
    }

    /**
     * 判断响应结果是否成功
     */
    public boolean isOk() {
        return this.code == 0;
    }

    public static <T> T data(CommonResult<T> commonResult) {
        if (!commonResult.isOk()) {
            throw new IllegalArgumentException(commonResult.getMessage());
        }
        return commonResult.getData();
    }

//    /**
//     * 将ApiResult转换成CommonResult
//     *
//     * @param apiResult
//     * @return
//     */
//    public static <T> CommonResult<T> of(ApiResult<T> apiResult) {
//        if (apiResult.isSuccess()) {
//            if (!"0".equals(apiResult.getResultCode())) {
//                return CommonResult.error(apiResult.getResultMessage(), apiResult.getResult());
//            }
//            return CommonResult.ok(apiResult.getResultMessage(), apiResult.getResult());
//        } else {
//            return CommonResult.error(apiResult.getResultMessage(), apiResult.getResult());
//        }
//    }
}
